package com.ihighest.app.infox.adapter;

import io.rong.imkit.RongIM;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;

public class GroupAdapter extends BaseAdapter {
	JSONArray array;
	Activity activity;
	LayoutInflater inflater = null;

	// protected ImageLoader imageLoader = ImageLoader.getInstance();

	public GroupAdapter(Activity activity, JSONArray array) {
		this.activity = activity;
		this.array = array;
		inflater = LayoutInflater.from(activity);

	}

	public GroupAdapter(Activity activity) {
		this.activity = activity;
		inflater = LayoutInflater.from(activity);

	}

	@Override
	public int getCount() {
		return array != null ? array.length() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View contextView, ViewGroup parent) {
		ViewHolder holder;
		if (contextView == null) {
			holder = new ViewHolder();
			contextView = inflater.inflate(R.layout.infox_activity_circlegroup_item, null);
			holder.rl_goto_group = (RelativeLayout) contextView.findViewById(R.id.rl_goto_group);
			holder.groupName = (TextView) contextView.findViewById(R.id.GroupName);
			holder.img = (ImageView) contextView.findViewById(R.id.GroupImg);
			holder.tv_group_ry_id = (TextView) contextView.findViewById(R.id.tv_group_ry_id);
			holder.tv_group_id = (TextView) contextView.findViewById(R.id.tv_group_id);
			holder.tv_group_join = (TextView) contextView.findViewById(R.id.tv_group_join);
			holder.tv_group_type = (TextView) contextView.findViewById(R.id.tv_group_type);
			holder.tv_group_bid = (TextView) contextView.findViewById(R.id.tv_group_bid);
			contextView.setTag(holder);
		} else {
			holder = (ViewHolder) contextView.getTag();
		}
		try {
			JSONObject json = array.getJSONObject(position);
			// Resources resources = contextView.getResources();
			// Drawable drawable =
			// resources.getDrawable(R.drawable.ic_launcher);
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(drawable).showImageOnFail(drawable)
			// .showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
			// imageLoader.displayImage(json.getString("image"), holder.img,
			// defaultOptions);
			AppKit.SetNormalImg(json.getString("image"), holder.img);
			holder.groupName.setText(json.getString("title"));
			holder.tv_group_id.setText(json.getString("id"));
			holder.tv_group_ry_id.setText(json.getString("rid"));
			holder.tv_group_join.setText(json.getBoolean("join") + "");
			holder.tv_group_type.setText(json.getString("type"));
			holder.tv_group_bid.setText(json.getString("bid"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		holder.rl_goto_group.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				boolean join = Boolean.valueOf(((TextView) v.findViewById(R.id.tv_group_join)).getText().toString());
				if (join) {
					if (TextUtils.equals("Activity", ((TextView) v.findViewById(R.id.tv_group_type)).getText())) {
						Const.IM_EXTRA = "group_activity:" + (String) ((TextView) v.findViewById(R.id.tv_group_bid)).getText() + ":"
								+ SharedPreferencesKit.getString(activity, Const.SP_KEY_USER_MID);
					} else {
						Const.IM_EXTRA = "";
					}

					RongIM.getInstance().startGroupChat(activity, ((TextView) v.findViewById(R.id.tv_group_ry_id)).getText().toString(),
							((TextView) v.findViewById(R.id.GroupName)).getText().toString());
				} else {
					ToastKit.showShort(activity, "尚未加入此群");
				}
			}
		});
		return contextView;
	}

	class ViewHolder {
		ImageView img;
		TextView groupName, tv_group_ry_id, tv_group_id, tv_group_join, tv_group_type, tv_group_bid;
		RelativeLayout rl_goto_group;
	}
}
